package com.arvin.faceDetect.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.arvin.faceDetect.databinding.ItemFaceBinding
import com.arvin.faceDetect.db.FaceEntity

class FaceAdapter : ListAdapter<FaceEntity, FaceAdapter.FaceViewHolder>(FaceDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FaceViewHolder {
        val binding = ItemFaceBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return FaceViewHolder(binding)
    }

    override fun onBindViewHolder(holder: FaceViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    class FaceViewHolder(
        private val binding: ItemFaceBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(face: FaceEntity) {
            binding.ivFace.setImageBitmap(face.faceImage)
            binding.tvName.text = face.name
        }
    }

    private class FaceDiffCallback : DiffUtil.ItemCallback<FaceEntity>() {
        override fun areItemsTheSame(oldItem: FaceEntity, newItem: FaceEntity): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: FaceEntity, newItem: FaceEntity): Boolean {
            return oldItem == newItem
        }
    }
} 